﻿//依赖库：angular，promise

    // 分页插件
cppei.paginationPlugin = {
    //
    // @value:页码
    // @text:文字
    // @tooptip:
    // @isSelected:是否选中（当前页）
    pageInfo: function (value, text, tooltip, isSelected) {
        this.value = value;
        this.text = text;
        this.tooltip = tooltip;
        this.isSelected = isSelected;
    },

    // 分页数据结构
    paginationdata: function (start, limit, curPage, count, maxPage, total) {
        this.total = total;
        this.maxPage = maxPage;
        this.curPage = curPage;
        this.count = count;
        this.start = start;
        this.limit = limit;
        this.pageInfos = [];
    },

    // 分页栏实例，每一个angular controller对应一个实例
    paginationInstance: function ($scope, $http, mod, sno, act, params) {
        this._mod = mod;
        this.sno = sno;
        this.act = act;
        this.params = params;
        this.paginationdata = null;
        // 成功获取数据之后的回调方法
        this.successFn = null;
        this.errorFn = null;

        // 注册成功之后的回调方法
        this.success = function (fn) {
            this.successFn = fn;
            return this;
        }

        // 注册失败之后的回调方法
        this.error = function (fn) {
            this.errorFn = fn;
            return this;
        }

        // 刷新，当前条件，当前页
        this.refresh = function () {
            var refreshParams = {};
            if (this.params) {
                refreshParams = this.params;
            }

            if (!refreshParams.page) {
                refreshParams.page = 0;
            }
            if (!refreshParams.limit) {
                refreshParams.limit = 15;
            }
            refreshParams._t = new Date().getTime();

            if (!this.params) {
                this.params = refreshParams;
            }

            var prms = Promise();
            prms.then(this.successFn, this.errorFn);

            $http({
                method: 'get',
                url: cppei.api.getUrl(this._mod, sno, act),
                cache: false,
                params: refreshParams
            }).success(function (data, status) {
                //
                if (data.isSuccess) {
                    // update state
                    //debugger;
                    $scope.paginationdata.total = data.data.total;

                    // debugger;
                    // 计算可用的页码
                    var maxPageIndex = parseInt(data.data.total / data.data.limit);
                    if (data.data.total % data.data.limit != 0) {
                        maxPageIndex++;
                    }

                    // 优化
                    $scope.paginationdata.pageInfos.splice(0, $scope.paginationdata.pageInfos.length);//清空 pageInfo

                    if (maxPageIndex > 6) {
                        //页码“1”
                        var pageInfo1 = new cppei.paginationPlugin.pageInfo(0, 1, 'p0', data.data.page == 0 ? true : false);
                        $scope.paginationdata.pageInfos.push(pageInfo1);

                        //省略号
                        var pageInfoPointPre = new cppei.paginationPlugin.pageInfo(-1, '...', '...', false);
                        $scope.paginationdata.pageInfos.push(pageInfoPointPre);

                        //中间页码
                        var centerPage = parseInt((maxPageIndex + 1) / 2) - 1;
                        for (var pageIndex = centerPage ; pageIndex < centerPage + 3; pageIndex++) {
                            var pageInfo = new cppei.paginationPlugin.pageInfo(pageIndex, pageIndex + 1, 'p' + pageIndex, data.data.page == pageIndex ? true : false);
                            $scope.paginationdata.pageInfos.push(pageInfo);

                        }

                        //省略号
                        var pageInfoPointAfter = new cppei.paginationPlugin.pageInfo(-1, '...', '...', false);
                        $scope.paginationdata.pageInfos.push(pageInfoPointAfter);

                        //最后一页
                        var pageInfolast = new cppei.paginationPlugin.pageInfo(maxPageIndex - 1, maxPageIndex, 'p' + (maxPageIndex - 1), data.data.page == (maxPageIndex - 1) ? true : false);
                        $scope.paginationdata.pageInfos.push(pageInfolast);

                    } else {
                        for (var pageIndex = 0; pageIndex < maxPageIndex; pageIndex++) {
                            var pageInfo = new cppei.paginationPlugin.pageInfo(pageIndex, pageIndex + 1, 'p' + pageIndex, data.data.page == pageIndex ? true : false);
                            $scope.paginationdata.pageInfos.push(pageInfo);
                        }
                    }

                    $scope.paginationdata.start = data.data.start;
                    $scope.paginationdata.limit = data.data.limit;
                    $scope.paginationdata.total = data.data.total;
                    $scope.paginationdata.maxPage = maxPageIndex - 1;
                    $scope.paginationdata.curPage = data.data.page;
                    $scope.paginationdata.count = data.data.count;

                    prms.resolve(data.data);
                } else {
                    prms.reject();
                }
            }).error(function (data, status) {
                prms.reject();
            });
        }

        // 跳转到某一页，不改变条件，只改变页码
        this.goto = function (pageIndex) {
            if (pageIndex < 0 || pageIndex > $scope.paginationdata.maxPage) {
                return;
            }
            if (pageIndex == $scope.paginationdata.curPage) {
                return;
            }

            var refreshParams = {};
            if (this.params) {
                refreshParams = this.params;
            }

            refreshParams.page = pageIndex;

            if (!refreshParams.limit) {
                refreshParams.limit = 15;
            }
            refreshParams._t = new Date().getTime();

            if (!this.params) {
                this.params = refreshParams;
            }

            var prms = Promise();
            prms.then(this.successFn, this.errorFn);
            $http({
                method: 'get',
                url: cppei.api.getUrl(this._mod, sno, act),
                cache: false,
                params: refreshParams
            }).success(function (data, status) {
                //
                if (data.isSuccess) {
                    // update state
                    //debugger;
                    $scope.paginationdata.total = data.data.total;

                    // debugger;
                    // 计算可用的页码
                    var maxPageIndex = parseInt(data.data.total / data.data.limit);
                    if (data.data.total % data.data.limit != 0) {
                        maxPageIndex++;
                    }

                    //debugger;

                    // 优化
                    $scope.paginationdata.pageInfos.splice(0, $scope.paginationdata.pageInfos.length);//清空 pageInfo

                    if (maxPageIndex > 6) {
                        var leftLength = refreshParams.page;
                        var rightLength = maxPageIndex - refreshParams.page;

                        //页码“1”
                        var pageInfo1 = new cppei.paginationPlugin.pageInfo(0, 1, 'p0', data.data.page == 0 ? true : false);
                        $scope.paginationdata.pageInfos.push(pageInfo1);

                        //中间页码
                        var centerPage = refreshParams.page - 1;

                        //上一页省略号
                        if (leftLength > 2) {
                            var pageInfoPointPre = new cppei.paginationPlugin.pageInfo(-1, '...', '...', false);
                            $scope.paginationdata.pageInfos.push(pageInfoPointPre);
                            //  centerPage = refreshParams.page - 1;
                        } else if (leftLength == 1) {
                            centerPage = refreshParams.page;
                        }
                        else if (leftLength == 0) {
                            centerPage = refreshParams.page + 1;
                        }

                        if (rightLength == 0) {
                            centerPage = refreshParams.page - 3;
                        }
                        else if (rightLength == 1) {
                            centerPage = refreshParams.page - 2;
                        }

                        for (var pageIndex = centerPage; (pageIndex < centerPage + 3) && (pageIndex < maxPageIndex - 1) && (pageIndex > 0) ; pageIndex++) {
                            var pageInfo = new cppei.paginationPlugin.pageInfo(pageIndex, pageIndex + 1, 'p' + pageIndex, data.data.page == pageIndex ? true : false);
                            $scope.paginationdata.pageInfos.push(pageInfo);

                        }

                        //下一页省略号
                        if (rightLength > 2) {
                            var pageInfoPointAfter = new cppei.paginationPlugin.pageInfo(-1, '...', '...', false);
                            $scope.paginationdata.pageInfos.push(pageInfoPointAfter);
                        }

                        //最后一页
                        if (maxPageIndex > 2) {
                            var pageInfolast = new cppei.paginationPlugin.pageInfo(maxPageIndex - 1, maxPageIndex, 'p' + (maxPageIndex - 1), data.data.page == (maxPageIndex - 1) ? true : false);
                            $scope.paginationdata.pageInfos.push(pageInfolast);
                        }
                    } else {
                        for (var pageIndex = 0; pageIndex < maxPageIndex; pageIndex++) {
                            var pageInfo = new cppei.paginationPlugin.pageInfo(pageIndex, pageIndex + 1, 'p' + pageIndex, data.data.page == pageIndex ? true : false);
                            $scope.paginationdata.pageInfos.push(pageInfo);
                        }
                    }


                    $scope.paginationdata.start = data.data.start;
                    $scope.paginationdata.limit = data.data.limit;
                    $scope.paginationdata.total = data.data.total;
                    $scope.paginationdata.maxPage = maxPageIndex - 1;
                    $scope.paginationdata.curPage = data.data.page;
                    $scope.paginationdata.count = data.data.count;

                    prms.resolve(data.data);
                } else {
                    prms.reject();
                }
            }).error(function (data, status) {
                prms.reject();
            });
        },

        // 重新加载，可以改变条件，回到第一页
        this.reload = function (params) {
            var refreshParams = {};
            if (params) {
                refreshParams = params;
                this.params = params;
            }

            if (!refreshParams.page) {
                refreshParams.page = 0;
            }

            if (!refreshParams.limit) {
                refreshParams.limit = 15;
            }
            refreshParams._t = new Date().getTime();

            if (!this.params) {
                this.params = refreshParams;
            }

            var prms = Promise();
            prms.then(this.successFn, this.errorFn);

            $http({
                method: 'get',
                url: cppei.api.getUrl(this._mod, sno, act),
                cache: false,
                params: refreshParams
            }).success(function (data, status) {
                // debugger;
                if (data.isSuccess) {
                    // update state
                    // debugger;
                    $scope.paginationdata.total = data.data.total;

                    // debugger;
                    // 计算可用的页码
                    var maxPageIndex = parseInt(data.data.total / data.data.limit);
                    if (data.data.total % data.data.limit != 0) {
                        maxPageIndex++;
                    }
                    // 优化
                    $scope.paginationdata.pageInfos.splice(0, $scope.paginationdata.pageInfos.length);//清空 pageInfo

                    if (maxPageIndex > 6) {
                        //页码“1”
                        var pageInfo1 = new cppei.paginationPlugin.pageInfo(0, 1, 'p0', data.data.page == 0 ? true : false);
                        $scope.paginationdata.pageInfos.push(pageInfo1);

                        //省略号
                        var pageInfoPointPre = new cppei.paginationPlugin.pageInfo(-1, '...', '...', false);
                        $scope.paginationdata.pageInfos.push(pageInfoPointPre);

                        //中间页码
                        var centerPage = parseInt((maxPageIndex + 1) / 2) - 1;

                        for (var pageIndex = centerPage ; pageIndex < centerPage + 3; pageIndex++) {
                            var pageInfo = new cppei.paginationPlugin.pageInfo(pageIndex, pageIndex + 1, 'p' + pageIndex, data.data.page == pageIndex ? true : false);
                            $scope.paginationdata.pageInfos.push(pageInfo);

                        }

                        //省略号
                        var pageInfoPointAfter = new cppei.paginationPlugin.pageInfo(-1, '...', '...', false);
                        $scope.paginationdata.pageInfos.push(pageInfoPointAfter);

                        //最后一页
                        var pageInfolast = new cppei.paginationPlugin.pageInfo(maxPageIndex - 1, maxPageIndex, 'p' + (maxPageIndex - 1), data.data.page == (maxPageIndex - 1) ? true : false);
                        $scope.paginationdata.pageInfos.push(pageInfolast);

                    } else {
                        for (var pageIndex = 0; pageIndex < maxPageIndex; pageIndex++) {
                            var pageInfo = new cppei.paginationPlugin.pageInfo(pageIndex, pageIndex + 1, 'p' + pageIndex, data.data.page == pageIndex ? true : false);
                            $scope.paginationdata.pageInfos.push(pageInfo);
                        }
                    }

                    $scope.paginationdata.start = data.data.start;
                    $scope.paginationdata.limit = data.data.limit;
                    $scope.paginationdata.total = data.data.total;
                    $scope.paginationdata.maxPage = maxPageIndex - 1;
                    $scope.paginationdata.curPage = data.data.page;
                    $scope.paginationdata.count = data.data.count;

                    prms.resolve(data.data);
                } else {
                    prms.reject();
                }
            }).error(function (data, status) {
                prms.reject();
            });
        }
    },

    // 附加到ng application, 创建directivemaxPageIndex
    attach2ngApp: function (app) {
        app.directive("pages", function () {
            return {
                restrict: 'E',
                template: '<div class="modal-footer no-margin-top">'
                    + '<div class="pull-left">从{{(paginationdata.start + 1)}}到{{(paginationdata.start+paginationdata.count)}}/共{{paginationdata.total}}条</div>'
                    + '<div class="pull-right">'
                       + '<ul class="pagination pull-right no-margin">'
                            //+ '<li ng-class="paginationdata.curPage==0?\'prev disabled\':\'\'">'
                            //   + '<a ng-click="paginationInstance.goto(0)" href="javascript:void(0);">'
                            //        + '<i class="icon-step-backward middle"></i></a></li>'
                            + ' <li ng-class="paginationdata.curPage==0?\'prev disabled\':\'\'">'
                                 + '<a ng-click="paginationInstance.goto(paginationdata.curPage-1)" href="javascript:void(0);">'
                                    + ' <i class="icon-caret-left middle"></i>'
                                 + '</a></li>'
                             + '<li ng-class="page.isSelected?\'active\':\'\'"  ng-repeat="page in paginationdata.pageInfos"  >'
                                + ' <a ng-click="paginationInstance.goto(page.value)"  href="javascript:void(0);" ng-disabled="(page.value==-1)">{{page.text}}</a></li>'
                            + ' <li ng-class="paginationdata.curPage==paginationdata.maxPage?\'prev disabled\':\'\'">'
                                 + '<a ng-click="paginationInstance.goto(paginationdata.curPage+1)" href="javascript:void(0);">'
                                     + '<i class="icon-caret-right middle"></i></a></li>'
                             //+ '<li ng-class="paginationdata.curPage==paginationdata.maxPage?\'prev disabled\':\'\'">'
                             //    + '<a ng-click="paginationInstance.goto(paginationdata.maxPage)" href="javascript:void(0);">'
                             //        + '<i class="icon-step-forward middle"></i></a></li>'
                          + '</ul></div></div>',
                replace: true
            }
        });
    },
    // 附加到controller，关联scope
    // @dataSourceParams:获取数据时使用的参数
    attach2ngScope: function ($scope, $http, mod, sno, act, params) {
        //debugger;
        $scope.paginationdata = new cppei.paginationPlugin.paginationdata();

        $scope.paginationInstance = new cppei.paginationPlugin.paginationInstance($scope, $http, mod, sno, act, params);

        return $scope.paginationInstance;
    }
};;
